﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using aspZone.Commerce.DataAccess;
using aspZone.Commerce.Model;
using System.Web.Caching;

namespace aspZone.Commerce.Web.App.Controllers
{
    public class MasterPageViewDataHelper
    {
        public static void AddViewDataRequiredByMasterPage(Cache cache, ViewDataDictionary viewData, ICategoryRepository categoryRepository)
        {
            var topMenuCategories = cache["TopMenuCategories"] as IList<Category>;
            if (topMenuCategories == null)
            {
                topMenuCategories = categoryRepository.GetForTopMenu();
                cache.Add("TopMenuCategories", topMenuCategories, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(5), CacheItemPriority.Normal, null);
            }
            viewData["TopMenuCategories"] = topMenuCategories;

            var categoryMenu = cache["CategoryMenu"] as aspZone.Commerce.Web.App.Models.Shared.CategoryMenu;
            if (categoryMenu == null)
            {
                categoryMenu = new aspZone.Commerce.Web.App.Models.Shared.CategoryMenu(categoryRepository.GetForCategoryMenu());
                cache.Add("CategoryMenu", categoryMenu, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(5), CacheItemPriority.Normal, null);
            }
            viewData["CategoryMenu"] = categoryMenu;

        }
    }
}
